<template>
<div>
  <div style="position: relative">
    <el-upload
        class="avatar-uploader"
        action
        ref="upload"
        :show-file-list="false"
        :auto-upload="false"
        :limit="1"
        :on-change="handleAvatarChangeIcon">
      <img v-if="dialogImageUrl" :src="dialogImageUrl" class="avatar testss">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <div class="picture-mask" v-if="dialogImageUrl">
      <i class="el-icon-zoom-in" style="margin-top: 65px;padding: 0 10px" @click="dialogVisiblePic=true"></i>
      <i class="el-icon-delete" style="margin-top: 65px;padding: 0 10px" @click="deletePic"></i>
    </div>
    <el-dialog :visible.sync="dialogVisiblePic">
      <img v-if="dialogImageUrl" :src="dialogImageUrl" style="max-width: 400px">
    </el-dialog>
  </div>
</div>
</template>

<script>
export default {
name: "upload-img",
  data(){
    return{
      dialogImageUrl:'',
      dialogVisiblePic:false
    }
  },

  methods:{
    handleAvatarChangeIcon(file,fileList){
      const isJPG = file.raw.type === 'image/jpeg'
      const isPNG = file.raw.type === 'image/png'
      const isLt2M = file.raw.size / 1024  < 500
      if (!isPNG && !isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG 格式!')
        return false
      } else if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 500KB!')
        return false
      } else if (isLt2M && (isPNG || isJPG)) {
        this.dialogImageUrl = URL.createObjectURL(file.raw);//图片的url
        this.$refs.upload.clearFiles();
        this.$emit('callImgFileFn',file.raw)
      }
    },

    // saveIco(file){
    //   let fd = new FormData();
    //   fd.append("file", file.raw);
    //
    // },

    deletePic(){
      this.dialogImageUrl = false
    },
  }
}
</script>

<style scoped>
.picture-mask:hover{
  opacity: 1;
}

.el-icon-zoom-in:hover{
  cursor: pointer;
}

.el-icon-delete:hover{
  cursor: pointer;
}

.picture-mask{
  position: absolute;
  width: 150px;
  height: 150px;
  left: 0;
  top: 0;
  cursor: default;
  text-align: center;
  color: #fff;
  opacity: 0;
  font-size: 20px;
  background-color: rgba(0,0,0,.5);
  transition: opacity .3s;
}

>>>.el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}



</style>